/*
 * Copyright (c) 2005, 2014 vacoor
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 */

/**
 * 一个简单的 grid 数据查询,查看面板
 *
 * @author Vacoor
 */
Ext.define('Nothing.ux.panel.DataViewer', {
    extend: 'Ext.panel.Panel',
    alias: 'widget.dataviewer',
    requires: ['Nothing.ux.form.SearchPanel', 'Nothing.ux.grid.DataGrid'],

    items: [],

    columns: [],

    store: null,

    split: true,

    border: false,

    /**
     *
     */
    autoGenItems: false,

    initComponent: function () {
        var me = this;

        me.items = me.genFieldUseColumns(me.columns, me.items, me.autoGenItems);
        delete me.autoGenItems;

        // ------------
        me.layout = 'border';
        me.items = [
            {
                region: 'north',
                xtype: 'searchpanel',
                header: false,
                collapseMode: 'mini',
                items: me.items,
                collapsible: true,
                collapsed: true,
                store: me.store,
                split: me.split
            },
            {
                region: 'center',
                margins: '0 5 5 5',
                xtype: 'datagrid',
                columns: me.columns,
                store: me.store,
                split: me.split,
                rownumbers: true,
                selType: 'rowmodel'
            }
        ];

        me.callParent(arguments);
    },

    /**
     * 根据 grid column 生成 form field item
     *
     * @param columns  grid columns
     * @param userItems user items
     * @param genFieldUseColumns 当true使用 column 生成并使用 userItems 覆盖, 否则使用 columns 填充 userItems fieldLabel
     * @returns {Array}
     */
    genFieldUseColumns: function (columns, userItems, genFieldUseColumns) {
        var items = [],
            userItemsCopy = Ext.apply([], userItems); // 这里做个副本进行修改, 否则会覆盖原来items, 下次创建还是这个对象会造成问题

        // 根据列生成
        if (genFieldUseColumns) {
            Ext.each(columns, function (column) {
                if (!column.dataIndex || false === column.searchable) {
                    return;
                }

                var i = 0,
                    item = { fieldLabel: column.header || column.text, name: column.dataIndex };

                for (; i < userItemsCopy.length; i++) {
                    if (item.name == userItemsCopy[i].name) {
                        Ext.apply(item, userItemsCopy[i]);

                        userItemsCopy.splice(i, 1);
                        break;
                    }
                }

                items.push(item);
            });
        } else {
            // 如果没有指定fieldLabel, 则自动根据 grid columns 生成 item 名称
            Ext.each(userItemsCopy, function (item) {

                Ext.each(columns, function (column) {
                    if (column.dataIndex && column.dataIndex == item.name) {
                        Ext.applyIf(item, { fieldLabel: column.header || column.text });
                        return false;
                    }
                });

            });
        }

        return items.concat(userItemsCopy);
    }
});
